package ae.pcfc.etks.online.web.controller.admin.general.user.role.update;

/* $Id: AdminUserRoleUpdateController 
 * Copyright (C) 2011, Trakhees
 *  
 *  History
 *
 * Date  			Author 					Remarks
 * -------------------------------------------------------------
 * April 21, 2011 	Neha_Chauhan 	        Created
 *
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.util.WebUtils;

import ae.pcfc.etks.online.common.delegate.AdminNonClientCompanyReviewDelegate;
import ae.pcfc.etks.online.common.delegate.AdminNonClientPersonReviewDelegate;
import ae.pcfc.etks.online.common.delegate.UserRoleDelegate;
import ae.pcfc.etks.online.common.delegate.UtilityDelegate;
import ae.pcfc.etks.online.common.vo.ClientCompanyVO;
import ae.pcfc.etks.online.common.vo.ClientPersonVO;
import ae.pcfc.etks.online.common.vo.ClientRoleVO;
import ae.pcfc.etks.online.common.vo.LabelVO;
import ae.pcfc.etks.online.common.vo.NonClientCompanyVO;
import ae.pcfc.etks.online.common.vo.NonClientPersonVO;
import ae.pcfc.etks.online.common.vo.UserVO;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.model.AdminUserRoleUpdate;
import ae.pcfc.etks.online.web.model.JQGridJSONModel;

/**
 * Controller that handles all {@link AdminUserRoleUpdateController}-related
 * requests, i.e.: all requests for admin/general/user/role/update/*
 * 
 * @author Neha Chauhan
 */

@Controller
@RequestMapping(ViewPath.ADMIN_GENERAL_USER_UPDATE_ROLE)
public class AdminUserRoleUpdateController extends BaseController{

	private UserRoleDelegate userRoleDelegate;

	private UtilityDelegate utilityDelegate;

	@Resource(name = "userRoleDelegate")
	public void setUserRoleDelegate(UserRoleDelegate userRoleDelegate) {
		this.userRoleDelegate = userRoleDelegate;
	}

	private AdminNonClientCompanyReviewDelegate adminNonClientCompanyReviewDelegate;

	@Resource(name = "adminNonClientCompanyReviewDelegate")
	public void setAdminNonClientCompanyReviewDelegate(AdminNonClientCompanyReviewDelegate adminNonClientCompanyReviewDelegate) {
		this.adminNonClientCompanyReviewDelegate = adminNonClientCompanyReviewDelegate;
	}

	private AdminNonClientPersonReviewDelegate adminNonClientPersonReviewDelegate;

	@Resource(name = "adminNonClientPersonReviewDelegate")
	public void setAdminNonClientPersonReviewDelegate(AdminNonClientPersonReviewDelegate adminNonClientPersonReviewDelegate) {
		this.adminNonClientPersonReviewDelegate = adminNonClientPersonReviewDelegate;
	}

	@Resource(name = "utilityDelegate")
	public void setUtilityDelegate(UtilityDelegate utilityDelegate) {
		this.utilityDelegate = utilityDelegate;
	}

	@RequestMapping(value = "/displayUsersTable")
	public @ResponseBody
	JQGridJSONModel displayUsersTable(HttpServletRequest request, @RequestParam(value = "page", required = true) String page,
			@RequestParam(value = "rows", required = true) String rows,
			@RequestParam(value = "sidx", required = true) String sidx,
			@RequestParam(value = "sord", required = true, defaultValue = "desc") String sord) {

		JQGridJSONModel result = new JQGridJSONModel();

		List<UserVO> userVOs = userRoleDelegate.getClientsList();
		System.out.println("userVOs->" + userVOs.toString());
		for (UserVO userVO : userVOs) {
			List<String> cells = new ArrayList<String>();
			cells.add(userVO.getStrUserName());
			cells.add(userVO.getStrEmail());
			cells.add(userVO.getStrAccountStatus());
			cells.add(userVO.getStrClientType());
			result.addRows("", cells);

		}

		Integer pageIndx = 1, count = 10, totalpages = 0, start = 0;
		Integer limit = 40;
		try {
			pageIndx = Integer.parseInt(page);
		} catch (Exception nullexp) {
			pageIndx = 1;
		}
		try {
			limit = Integer.parseInt(rows);
		} catch (Exception nullexp) {
			pageIndx = 10;
		}

		if (count > 0)
			totalpages = (int) (count) / limit;
		else
			totalpages = 0;

		if (pageIndx > totalpages)
			pageIndx = totalpages;

		start = (limit * pageIndx) - limit;
		result.setPage(pageIndx.toString());
		result.setRecords(limit + "");
		result.setTotal(totalpages + "");

		return result;

	}

	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * admin/general/user/role/update/form.jsp
	 * 
	 * @see WEB-INF/jsp/admin/general/user/role/update/form.jsp
	 */

	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public void form(@ModelAttribute(value = ConstantsAdmin.ADMIN_UPDATE_USER_ROLE_MODEL_KEY) AdminUserRoleUpdate command) {

	}
	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * admin/general/user/role/update/show.jsp
	 * 
	 * @see WEB-INF/jsp/admin/general/user/role/update/show.jsp
	 */
	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public void show(HttpServletRequest request,
			@ModelAttribute(value = ConstantsAdmin.ADMIN_UPDATE_USER_ROLE_MODEL_KEY) AdminUserRoleUpdate command) {

	}

	@ModelAttribute(value = ConstantsAdmin.COMPANY_ADMIN_UPDATEROLE_MODEL_KEY)
	public NonClientCompanyVO populateDataCompany(@RequestParam(value = "id", required = false) String id) {

		if (id != null) {
			NonClientCompanyVO nonClientCompanyVO = adminNonClientCompanyReviewDelegate
					.getNonClientCompanyByApplicationId(id);

			if (nonClientCompanyVO != null)
				return nonClientCompanyVO;

		}

		return new NonClientCompanyVO();
	}

	@ModelAttribute(value = ConstantsAdmin.PERSON_ADMIN_UPDATEROLE_MODEL_KEY)
	public NonClientPersonVO populateDataPerson(@RequestParam(value = "id", required = false) String id) {

		if (id != null) {
			NonClientPersonVO nonClientPersonVO = adminNonClientPersonReviewDelegate.getNonClientPersonByApplicationId(id);

			if (nonClientPersonVO != null)
				return nonClientPersonVO;

		}

		return new NonClientPersonVO();

	}

	@ModelAttribute(value = ConstantsAdmin.ADMIN_UPDATEROLE_ADMINDETAILS_C_MODEL_KEY)
	public ClientCompanyVO populateAdminDataCompany(@RequestParam(value = "id", required = false) String id) {

		return new ClientCompanyVO();
	}

	@ModelAttribute(value = ConstantsAdmin.ADMIN_UPDATEROLE_ADMINDETAILS_P_MODEL_KEY)
	public ClientPersonVO populateAdminDataPerson(@RequestParam(value = "id", required = false) String id) {

		return new ClientPersonVO();
	}

	@ModelAttribute(ConstantsAdmin.NON_CLIENT_COMPANY_REGISTRATION_CLIENT_ROLES_LIST_REQUEST_MODEL_KEY)
	public List<LabelVO> populateRolesList() {

		List<LabelVO> labelList = new ArrayList<LabelVO>();
		List<UserVO> userVOs = userRoleDelegate.getClientsList();
		String strClientType = "";
		Iterator userNew = userVOs.iterator();
		while (userNew.hasNext()) {
			UserVO userVO = (UserVO) userNew.next();
			strClientType = userVO.getStrClientType();
		}
		LabelVO labelVO;

		if (strClientType.equals("C")) {
			List<ClientRoleVO> clientRoleVOs = utilityDelegate.getClientRoleList("C",true);
			for (ClientRoleVO roleVO : clientRoleVOs) {
				labelVO = new LabelVO();
				labelVO.setId(roleVO.getStrCrId());
				labelVO.setLabelAr(roleVO.getStrCrDescArb());
				labelVO.setLabelEn(roleVO.getStrCrDesc());
				labelList.add(labelVO);

			}
		}
		if (strClientType.equals("P")) {
			List<ClientRoleVO> clientRoleVOs = utilityDelegate.getClientRoleList("P", true);
			for (ClientRoleVO roleVO : clientRoleVOs) {
				labelVO = new LabelVO();
				labelVO.setId(roleVO.getStrCrId());
				labelVO.setLabelAr(roleVO.getStrCrDescArb());
				labelVO.setLabelEn(roleVO.getStrCrDesc());
				labelList.add(labelVO);

			}
		}
		return labelList;
	}

	@ModelAttribute(ConstantsAdmin.ADMIN_LOCK_UNLOCK_REQUEST_MODEL_KEY)
	public List<LabelVO> populateLockUnlockList() {

		List<LabelVO> labelList = new ArrayList<LabelVO>();

		LabelVO labelVO = new LabelVO();
		labelVO.setId("1");
		labelVO.setLabelAr("Lock - Ar");
		labelVO.setLabelEn("Lock");
		labelList.add(labelVO);

		labelVO = new LabelVO();
		labelVO.setId("2");
		labelVO.setLabelAr("Unlock - Ar");
		labelVO.setLabelEn("Unlock");
		labelList.add(labelVO);

		return labelList;
	}

	/**
	 * Custom handler for displaying the form, i.e.: POST requests for
	 * admin/general/user/role/update/form.jsp
	 * 
	 * @see WEB-INF/jsp/admin/general/user/role/update/form.jsp
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String form(HttpServletRequest request, HttpSession session, SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.ADMIN_UPDATE_USER_ROLE_MODEL_KEY) @Valid AdminUserRoleUpdate userUpdateInfo,
			BindingResult errors) {

		boolean isSearch = WebUtils.hasSubmitParameter(request, ConstantsAdmin.SEARCH_BUTTON);

		if (isSearch) {
			if (errors.hasErrors()) {

				return ViewUtils.DEFAULT_VIEW;

			} else {
				// extract the search criteria here
				// adminDelegate.getMapper().map(userUpdateInfo,
				// userUpdateInfo);
			}
		} else {
			String strDetails = request.getParameter("strEmpDetails");
			session.setAttribute("strDetails", strDetails);
			return ViewUtils.getRedirect(ViewPath.ADMIN_GENERAL_USER_UPDATE_ROLE, PageType.SHOW);
		}
		status.setComplete();
		return ViewUtils.getRedirect(ViewPath.ADMIN_GENERAL_USER_UPDATE_ROLE, PageType.SHOW);
	}

	/**
	 * Custom handler for displaying the form, i.e.: POST requests for
	 * admin/general/user/role/update/show.jsp
	 * 
	 * @see WEB-INF/jsp/admin/general/user/role/update/show.jsp
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/show.htm")
	public String show(HttpServletRequest request, SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.ADMIN_UPDATE_USER_ROLE_MODEL_KEY) @Valid AdminUserRoleUpdate userUpdateInfo,
			BindingResult errors) {

		boolean isSave = WebUtils.hasSubmitParameter(request, ConstantsAdmin.SAVE_BUTTON);

		if (isSave) {
			if (errors.hasErrors()) {

				return ViewUtils.DEFAULT_VIEW;

			} else {

				// adminDelegate.getMapper().map(userUpdateInfo,
				// userUpdateInfo);
			}
		}

		return ViewUtils.getRedirect(ViewPath.ADMIN_GENERAL_USER_UPDATE_ROLE, PageType.SHOW);
	}

}
